/**
 * 详情对话框
 */
var ZxChengbenMingxiInfoDlg = {
    data: {
        btyZxChengbenId: "",
        btyZxMdzxId:"",
        btyZxKemuId: "",
        jine: "",
        beizhu: "",
        status: "",
        statusView: "",
        delFlag: "",
        createUser: "",
        createUserName: "",
        createTime: "",
        updateUser: "",
        updateUserName: "",
        updateTime: "",
        company: "",
        companyView: "",
        srcType: "",
        srcValue: "",
        gongshi: ""
    }
};
/**
 * 科目详情对话框
 */
var ZxKemuInfoDlg = {
    data: {
        btyZxKemuId: "",
        name: ""
    }
};


layui.use(['layer','form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    form.verify({
        double: function (value) {
            var patt2=/^(\-|\+)?\d+(\.\d+)?$/;
            if (patt2.test(value) == false) {
                return '你输入的不是数字';
            }
        }
    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/zxChengbenMingxi/detail?btyZxChengbenMingxiId=" + Feng.getUrlParam("btyZxChengbenMingxiId"));
    var result = ajax.start();
    form.val('zxChengbenMingxiForm', result.data);


    $("#btyZxKemuId").val(Feng.getUrlParam("btyZxKemuId"));
    // $("#btyZxKemuIdView").val(decodeURIComponent(Feng.getUrlParam("btyZxKemuIdView")));
    form.render('select');
    function closePage(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/zxChengbenMingxi/editItem2", function (data) {
            if (data.code != 200) {
                Feng.error(data.message);
            }else {
                Feng.success("更新成功！");
                closePage();
            }
            // window.location.href = Feng.ctxPath + '/zxChengbenMingxi?btyZxChengbenId='+Feng.getUrlParam("btyZxChengbenId")
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });

        var btyyBxdaIdView = $("#btyyBxdaId").find("option:selected").text();
        ajax.set("btyyBxdaIdView", btyyBxdaIdView.split("-")[0]);
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        closePage();
        // window.location.href = Feng.ctxPath + '/zxChengbenMingxi?btyZxChengbenId='+Feng.getUrlParam("btyZxChengbenId")
    });

    // 点击科目时
    $('#btyZxKemuIdView').click(function () {
        var formName = encodeURIComponent("parent.ZxKemuInfoDlg.data.name");
        var formId = encodeURIComponent("parent.ZxKemuInfoDlg.data.btyZxKemuId");
        var treeUrl = encodeURIComponent("/zxKemu/tree");

        layer.open({
            type: 2,
            title: '科目选择',
            area: ['300px', '345px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#btyZxKemuId").val(ZxKemuInfoDlg.data.btyZxKemuId);
                $("#btyZxKemuIdView").val(ZxKemuInfoDlg.data.name);
            }
        });
    });

    /**
     * 初始门店
     */
    function init_btyZxMdzxId(){
        var languageAjax = new $ax(Feng.ctxPath + "/mdzx/mdzxList");
        var languageResult = languageAjax.start();
        $.each(languageResult.data, function (index, val) {
            if(val.btyBrandIdView!=undefined){
                var pinyin = ConvertPinyin(val.btyBrandIdView);
                //添加的门店
                $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView +"-"+ pinyin + "</option>");
            }
        });
        $('#btyZxMdzxId').val(result.data.btyZxMdzxId);
        form.render('select');
    }

    init_btyZxMdzxId();

    /**
     * 初始化厂家
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getAllVendorList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        var pinyin = ConvertPinyin(val.xm);
        $("#btyyBxdaId").append("<option value='" + val.yhk + "'>" + val.xm +"-"+ val.yhk+"-"+ pinyin + "</option>");
    });

    $('#btyyBxdaId').val(result.data.btyyBxdaId);

    form.render('select');

    //选择厂家时，1.将厂家对应的名称赋值给厂家名称 2.将厂家的id(即银行卡号，赋值给银行卡号)
    form.on('select(btyyBxdaId)',function (data) {
        var btyyBxdaIdView= $("#btyyBxdaId").find("option:selected").text();
        $("#btyyBxdaIdView").val("");
        if(btyyBxdaIdView!="请选择") {
            $("#btyyBxdaIdView").val(btyyBxdaIdView);
        }

    });



    /**
     * 初始关键字-其它科目-三级科目
     */
    var languageAjax = new $ax(Feng.ctxPath + "/zxKemuKey/getAllKemuKeyList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        if(val.keyword!=undefined){
            var pinyin = ConvertPinyin(val.keyword);
            //添加的二级科目id及关键字（三级科目）
            $("#keywordId").append("<option value='" + val.id + "'>" + val.keyword +"-"+ pinyin + "</option>");
        }
    });
    $('#keywordId').val(result.data.keywordId);
    form.render('select');
    $('#status').val(result.data.status);
    form.render('select');

    var isCheck = false;
    if(result.data.companyView!=""){
        var cj_je = parseFloat(result.data.companyView);
        if(cj_je>0){
            $("#choudian").prop("checked", true);
            form.render('checkbox');
            isCheck=true;
        }
    }

    function calChouddian(){
        if(isCheck){
            var zongjia = $("#jine").val();
            if(zongjia==""){
                zongjia="0";
            }
            var zongjia_d = parseFloat(zongjia);
            var xishu = parseFloat("0.05");
            var choudian_je = zongjia_d*xishu;
            $('#companyView').val(choudian_je.toFixed(2));
        }else {
            $('#companyView').val("0.00");
        }
    }

    form.on('checkbox(choudian)', function (data) {
        isCheck = data.elem.checked;
        calChouddian();
    });

    $("#shuliang").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var unitPrice = $("#unitPrice").val();
        if(unitPrice==""){
            $("#jine").val("");
            return;
        }
        var jine = shuliang * unitPrice;
        $("#jine").val(jine.toFixed(2));
        calChouddian();
    });
    $("#unitPrice").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var unitPrice = $("#unitPrice").val();
        if(unitPrice==""){
            $("#jine").val("");
            return;
        }
        var jine = shuliang * unitPrice;
        $("#jine").val(jine.toFixed(2));
        calChouddian();
    });
});
